import java.awt.Point;
import java.io.IOException;
import java.io.*;

/**
 * Class that describe the Main process of our solution.
 * This class provide to call all the method necessary in order to solve the solution,basically 
 * describes the solution process of our implementation.
 * It takes one parameter as input consisting of our file.
 *  
 */

public class Main {

	public static final String PATH = "./src/";
	public static final String FILE_INPUT = "codeRule.txt";
	public static final String FILE_OUTPUT = "result.txt";

	public static void main(String[] args) {
		try {
			Encoder encoder;
//			if(args[0] != "0"){
//				encoder = new Encoder(args[0]);
//			}
			 //Encoder encoder = new Encoder("examples/example1.txt");
		     //Encoder encoder = new Encoder("examples/example2.txt");
			 //Encoder encoder = new Encoder("examples/example3.txt");
		     //Encoder encoder = new Encoder("examples/example4.txt");
			encoder = new Encoder("examples/example5.txt");
		     //Encoder encoder = new Encoder("examples/example6.txt");
			 //Encoder encoder = new Encoder("examples/example7.txt");
			 //Encoder encoder = new Encoder("examples/example8.txt");
			
		    System.out.println(encoder.getGraph().toString());
            Solver satSolver = new Solver(encoder.getGraph());
		   	Decoder decoder = new Decoder();

            satSolver.defineRuleNew();
			satSolver.execute();

			decoder.decode();
		
        	System.out.println(decoder.getHamiltonianPath().toString());
        	
        	Graphviz gv = new Graphviz();
			gv.addln(gv.start_graph());
			boolean isPath;
			int i = 1;
			for (Point e : encoder.getGraph().E) {
				isPath = false;
				for (Point n : decoder.getHamiltonianPath().E) {
					if(e.x==n.x&&e.y==n.y||e.x==n.y&&e.y==n.x){
						isPath = true;
						break;
					}
				}
				if(isPath){
					gv.addln((int)e.getX() + " -> " + (int)e.getY() + " [label=\""+i+"\" dir=none color=\"red\" style=bold];");
				}else{
					gv.addln((int)e.getX() + " -> " + (int)e.getY() + " [label=\""+i+"\" dir=none];");
				}
				i++;
			}
			gv.addln(gv.end_graph());
			System.out.println(gv.getDotSource());
			Writer out = new BufferedWriter(new FileWriter("src/result.dot"));
			try{
				out.write(gv.getDotSource());
			}
			finally {
				out.close();
			}

			//String type = "png";
			//File out = new File("tmp/out." + type);
			//gv.writeGraphToFile( gv.getGraph( gv.getDotSource(), type ), out );

		} catch (IOException e) {
			System.out.println(e.getMessage());
		} catch (IndexOutOfBoundsException ex) {
			System.out.println(ex.getMessage());
		}

	}
}
